package com.example.demo.controller.voj;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.IdParam;
import com.example.demo.domain.entity.NewCheckpoint;
import com.example.demo.domain.entity.TrainingProblem;
import com.example.demo.dto.ProblemViewDto;
import com.example.demo.dto.ProblemViewPageDto;
import com.example.demo.dto.ResponseDto;
import com.example.demo.param.voj.VojCreateProblemParam;
import com.example.demo.param.voj.VojProblemQueryParam;
import com.example.demo.service.NewProblemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题库模块-编程题
 */
@Controller
@RequestMapping("/problem")
public class ProblemController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProblemController.class);

    @Autowired
    private NewProblemService newProblemService;

    /**
     * 创建、保存、禁用试题
     * @return 包含试题创建结果的 Map<String, Boolean>对象
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResponseDto<Long> save(@RequestBody VojCreateProblemParam param) {
        Long result = newProblemService.save(param);
        ResponseDto<Long> response = new ResponseDto<Long>().buildSucceedHasData(result);
        return response;
    }

    /**
     * 删除题目
     * @param
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseDto delete(@RequestBody IdParam param) {
        ResponseDto<Object> responseDto = new ResponseDto<>();
        int result = newProblemService.delete(param.getId());
        if(result == 0) {
            responseDto.setSuccess(false);
        }
        return responseDto;
    }

    /**
     * 列表页
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public ResponseDto<Page<ProblemViewDto>> list(@RequestBody VojProblemQueryParam param) {
        Page<ProblemViewDto> data = newProblemService.list(param);
        ResponseDto result = new ResponseDto<>().buildSucceed();
        result.setContent(data);
        return result;
    }
    /**
     * 详情页
     * @return
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseDto<TrainingProblem> detail(@RequestBody IdParam param) {
        TrainingProblem problem = newProblemService.detail(param.getId());
        ResponseDto result = new ResponseDto<>().buildSucceed();
        result.setContent(problem);
        return result;
    }

    /**
     * 通过problemid获取数据用例
     * @return
     */
    @RequestMapping("/getProblemCheckPointByProblemId")
    @ResponseBody
    public ResponseDto<List<NewCheckpoint>> getProblemCheckPointByProblemId(@RequestBody IdParam param) {
        List<NewCheckpoint> data = newProblemService.getCheckpointsUsingProblemId(param.getId());
        ResponseDto result = new ResponseDto<>().buildSucceed();
        result.setContent(data);
        return result;
    }



}
